package com.ruoyi.mp.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.api.domain.MpAccount;
import com.ruoyi.mp.api.domain.MpGetCode;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公众号账号Controller
 * @author 郎兴宇
 * @date 2024-09-15
 */
@RestController
@RequestMapping("/account")
public class MpAccountController extends BaseController
{
    @Autowired
    private IMpAccountService mpAccountService;
    @Autowired
    private IMpAccountService iMpAccountService;
    @Autowired
    private WxMpService wxService;
    /**
     * 查询公众号账号列表
     */
    @RequiresPermissions("mp:account:list")
    @GetMapping("/list")
    public TableDataInfo list(MpAccount mpAccount)
    {
        startPage();
        List<MpAccount> list = mpAccountService.selectMpAccountList(mpAccount);
        return getDataTable(list);
    }

    /**
     * 导出公众号账号列表
     */
    @RequiresPermissions("mp:account:export")
    @Log(title = "公众号账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MpAccount mpAccount)
    {
        List<MpAccount> list = mpAccountService.selectMpAccountList(mpAccount);
        ExcelUtil<MpAccount> util = new ExcelUtil<MpAccount>(MpAccount.class);
        util.exportExcel(response, list, "公众号账号数据");
    }

    /**
     * 获取公众号账号详细信息
     */
    @RequiresPermissions("mp:account:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mpAccountService.selectMpAccountById(id));
    }

    /**
     * 新增公众号账号
     */
    @RequiresPermissions("mp:account:add")
    @Log(title = "公众号账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MpAccount mpAccount)
    {
        return toAjax(mpAccountService.insertMpAccount(mpAccount));
    }

    /**
     * 修改公众号账号
     */
    @RequiresPermissions("mp:account:edit")
    @Log(title = "公众号账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MpAccount mpAccount)
    {
        return toAjax(mpAccountService.updateMpAccount(mpAccount));
    }

    /**
     * 删除公众号账号
     */
    @RequiresPermissions("mp:account:remove")
    @Log(title = "公众号账号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mpAccountService.deleteMpAccountByIds(ids));
    }

    /**
     * 获取公众号的二维码图片
     */
    @RequiresPermissions("mp:account:getUrl")
    @Log(title = "获取公众号二维码")
    @GetMapping("/getUrl/{id}")
    public R getUrl(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        int sceneId = 132;
        Integer time = 300;
        WxMpQrCodeTicket wxMpQrCodeTicket =  wxService.switchoverTo(appId).getQrcodeService().qrCodeCreateTmpTicket(sceneId,time);
        MpAccount mpAccount = new MpAccount();
        mpAccount.setQrCodeUrl(wxService.switchoverTo(appId).getQrcodeService().qrCodePictureUrl(wxMpQrCodeTicket.getTicket()));
        mpAccount.setId(id);
        return R.ok(mpAccountService.updateMpAccount(mpAccount));
    }

    /**
     * api次数清零
     */
    @RequiresPermissions("mp:account:clearQuota")
    @Log(title = "api次数清零")
    @PostMapping("/clearQuota/{id}")
    public R clearQuota(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        wxService.switchoverTo(appId).clearQuota(appId);
        return  R.ok(0);
    }


    /**
     * 获取公众号带参数的二维码以及返回给调用服务
     * seesionId websocket的id
     * time 二维码的换取时间
     */
    @InnerAuth
    @RequiresPermissions("mp:account:getUrl")
    @Log(title = "获取公众号二维码")
    @PostMapping("/getcodeUrl")
    public R<String> mpGetcodeUrl(@RequestBody MpGetCode mpGetCode) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(mpGetCode.getMpId().longValue()).getAppId();
        WxMpQrCodeTicket wxMpQrCodeTicket =  wxService.switchoverTo(appId).getQrcodeService().qrCodeCreateTmpTicket(mpGetCode.getSeesionid(),mpGetCode.getTime());
        return R.ok(wxService.switchoverTo(appId).getQrcodeService().qrCodePictureUrl(wxMpQrCodeTicket.getTicket()));
    }
}
